<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	//Doctrine EntityManager
	public $em;

	function __construct() {
		parent::__construct();

		//Not required if you autoload the library
		$this -> load -> library('doctrine');
		
		//Instantiate a Doctrine Entity Manager
		$this -> em = $this -> doctrine -> em;
		// $conn = $this->em->getConnection();
		//var_dump(models\service\CurrentUser::user());
		
	}
		 
	public function index()
	{
		$this->load->view('login');
	}
	
	public function home(){
		if (models\service\CurrentUser::user() == FALSE){
			header('location: ' . base_url());
		}
		$user = models\service\CurrentUser::user();
		$criteria = array('userId' => $user->getUserId());
		$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findBy($criteria);
		$data['userProfiles'] = $userProfiles;
		$this->load->view('welcome/index', $data);
	}
	
	public function sendTestMail(){
		$this->load->library('email'); // Note: no $config param needed
		$this->email->from('camplenkku@gmail.com', 'Adri Yudo');
		$this->email->to('nanda.sirajulmunir@solidaritas.com', 'abdul.basit@solidaritas.com');
		$this->email->cc('adri.yudo@solidaritas.com');
		$this->email->subject('Hai ini adalah email testing');
		$this->email->message('Halo teman-teman, apakabar? ini email dari Aplikasi mis lho...');
		$this->email->send();
	}
	
	public function sendTest($idBerkas){
		$lastBerkasComment = $this->em->getRepository('\models\entities\BerkasComment')->findLastBerkasComment($idBerkas);
		echo "<pre>"; print_r($lastBerkasComment);
	}
	
	public function testMailReview(){
		$reviewer = $this -> em -> getRepository('\models\entities\Berkas') -> findPerpustakaanProcedureStepProfilReviewer();
		$emails = array();
		$users = array();
		
		foreach ($reviewer as $review){
			if ($review['procedureStep'] == 3){
				// get  
				$cbos = $this->em->getRepository('\models\entities\Perpustakaan')->findCBOByPerpustakaan($review['perpustakaanId']);
				foreach ($cbos as $cbo){
					if (!in_array($cbo['mail'],$users)){
						array_push($emails, $cbo['user_id']);
					}	
				}
				
			} else {
				$criteria = array('id' => $review['profile']);
				$profile = $this->em->getRepository('\models\entities\Profile')->findBy($criteria);
				$criteria = array('profile' => $profile);
				$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findBy($criteria);
				foreach ($userProfiles as $up){
					// $criteria = array('userId' => $up->getUserId());
					// $userp = $this->em->getRepository('\models\entities\User')->findOneBy($criteria);
					// if (!in_array($userp->getMail(), $emails)){
					if (!in_array($up->getUserId(), $users)){	
						array_push($users, $up->getUserId());
						// $name = $userp->getName();
						// if ($name == null){
							// $name = 'noname';
						// }
						// $emails[$name] = $userp->getMail();
					}
				}
			}
		}	
	
		if (count($emails) > 0){
			$notifService = new models\service\NotificationService;
			//$notifService -> sendMail($berkasId);
			//echo "<pre>";print_r($users);die();
			foreach ($users as $key=>$val){
				$notifService->sendMailByMailAndMessage($val);
				// if ($key == 'noname'){
					// $notifService->sendMailByMailAndMessage($val);
				// } else {
					// $notifService->sendMailByMailAndMessage($val, $key);	
				// }
			}
		}	
	}

	function testExportXls(){
		$this->load->library('excel',array('filename' => 'test.xls'));
		
		$header = "Laporan Kegiatan";
		$this->excel->addHeader($header);
		
		$header = null;
		$this->excel->addHeader($header);
				
		$header[] = "ID";
		$header[] = "PERPUSTAKAAN";
		// $header[] = "DESCRIPTION";
		$header[] = "RESULT";
		$header[] = "MULAI";
		$header[] = "SELESAI";
		$header[] = "REPORTER";
		$header[] = "VERIFIER";
		$header[] = "APPROVER";
		// $header[] = "HASIL KEGIATAN";
		$this->excel->addHeader($header);
		
		$berkas = $this -> em -> getRepository('\models\entities\Berkas') -> findToExportXls();

		foreach ($berkas as $value) {
			$this->excel->addRow(array(
				$value['berkasId'],
				$value['perpustakaanName'],
				// $value['berkasDescription'],
				$value['berkasResult'],
				$value['berkasMulai'],
				$value['berkasSelesai'],
				$value['berkasReporter'],
				$value['berkasVerifier'],
				$value['berkasApprover'],
				// $value['berkasHasilKergiatan']
				));
		}
		
		// print_r($this->excel->returnSheet());die;

		$this->excel->sendFile();
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */